//
//  LoopNetWorkManager.swift
//  LoopWeibo
//
//  Created by loop安 on 2016/12/8.
//  Copyright © 2016年 loop. All rights reserved.
//

import UIKit
import AFNetworking
enum HTTPMethod:Int {
    case GET = 0
    case POST
}
class LoopNetWorkManager: AFHTTPSessionManager {
    
    static let shared:LoopNetWorkManager = {
    let manager = LoopNetWorkManager()
        manager.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return manager
    }()
    
    
    //get,post都可以调用这个方法
    //url
    //参数
    //闭包回调
    func request(HTTPMethod:HTTPMethod,urlString:String,parameters:Any?,finish:@escaping (Any?,Error?)->()){
        
        let successCallBack = {(task:URLSessionDataTask,responseObj:Any?)->() in
        finish(responseObj,nil)
        }
        
        let failedCallBack = {(task:URLSessionDataTask?,error:Error)->() in
        finish(nil,error)
        }
        
        //判断,选择请求方法
        if HTTPMethod == .GET {
            
            self.get(urlString, parameters: parameters, progress: nil, success: successCallBack, failure: failedCallBack)
        }else{
            self.post(urlString, parameters: parameters, progress: nil, success: successCallBack, failure: failedCallBack)
        }
        
    }

}
